$.ajaxSetup({
	error: function(xhr){
		var json = JSON.parse(xhr.responseText);
		layer.alert(json.message, {icon:2});
	}
});

//全局变量：count.秒数；accountExist.用户名是否绑定；cellphoneExists.手机号是否被绑定;code.验证码是否正确；phone.手机号是否一致
//telReg:手机号验证正则
var global = {count:60,accountExists:false,cellphoneExists:false,codeValid:false,phone:""},
	telReg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
//验证封装
var verification = {

	main:function(){
		var me = this;

		me.bindEvent();
		
		if($(".regist_tip input").prop("checked")){
			$(".regist_fun").show();
			$(".noregist_fun").hide();
		} else {
			$(".noregist_fun").show();
			$(".regist_fun").hide();
		}

		// 切换企业，个人用户
		$(".regist_form_head div").click(function(){
			$(".regist_form_head div").removeClass("border_color");
			$(this).addClass("border_color");
			$("#accountType").val($(this).text());
		});

		// 注册点击
		$(".regist_fun").click(function(){
			//var result = me.registValidation();
			//if(result){
				$.post(root+"/register/new", {
					accountNo:$.trim($("#account").val()),
					password:$.trim($("#password").val()),
					cellphone:$.trim($("#cellphone").val()),
					accountType:$.trim($("#accountType").val()),
					vercode:$.trim($("#vercode").val())
				}).success(function(json){
					location.href = root+"/index.html";
				});
			//}
		});
		
		// 灰注册点击
		$(".noregist_fun").click(function(){
			layer.alert("请先同意集成规范！",{icon:2});
		});
		
		//点击“同意集成规范”
		$(".regist_tip input").click(function(){
			if($(this).prop("checked")){
				$(this).prop("checked",true);
				$(".noregist_fun").hide();
				$(".regist_fun").show();
			}else{
				$(this).prop("checked",false);
				$(".noregist_fun").show();
				$(".regist_fun").hide();
			}
		})
	},
	

	//绑定表单失焦的验证方法以及获取验证码
	bindEvent:function(){
		
		//用户名，企业名是否存在
		$("#account").blur(function(){
			var account = $.trim($(this).val());
			account&&checkAccountExists(account);
			function checkAccountExists(account){
				$.get(root+"/register/checkAccountExists",{account:account}).success(function(json){
					if(json.accountExists) {
						layer.alert("用户账号已存在！", {icon:2});
						global.accountExists = true;
					} else {
						global.accountExists = false;
					}
				});
			}
		}),
		
		
		//手机号是否存在
		$("#cellphone").blur(function(){
			var cellphone = $.trim($(this).val());
			cellphone&&checkCellphoneExists(cellphone);
			function checkCellphoneExists(cellphone){
				$.get(root+"/register/checkCellphoneExists",{cellphone:cellphone}).success(function(json){
					if(json.cellphoneExists) {
						layer.alert("手机号已注册！", {icon:2});
						global.cellphoneExists = true;
					} else {
						global.cellphoneExists = false;
					}
				});
			}
		}),
		
		//判断验证码是否正确
		$("#vercode").blur(function(){
			var vercode = $.trim($(this).val());
			var cellphone = $.trim($("#cellphone").val());
			vercode&&cellphone&&check_vercode(vercode,cellphone);
			function check_vercode(vercode,cellphone){
				$.get(root+"/register/validateSmsCode",{vercode:vercode,cellphone:cellphone}).success(function(json){
					if(json.isValid){
						global.codeValid = true;
					}else{
						layer.alert("验证码错误！", {icon:2});
						global.codeValid = false;
					}
				});
			}
		}),
		
		
		//获取验证码
		$("#btnCode").click(function(){
			var account = $.trim($("#account").val());
			var cellphone = $.trim($("#cellphone").val());
			if(!account) {
				layer.alert("您还没有填写账号名！", {icon:2});
				return;
			}			
			if (!cellphone) {
				layer.alert("手机号码为空！",{icon:2});
				return;
			}
			if(!telReg.test(cellphone)){
				layer.alert('手机号格式不正确！', {icon:2});
				return;
			}
			global.phone = cellphone;//用于提交时验证，确保提交时用户没有修改获取验证码的手机号
			sendSmsCodeRemote(account,cellphone);
			
			//发送验证码
			function sendSmsCodeRemote(account,cellphone) {
				$.get(root+"/register/sendSmsCode", {account:account,cellphone:cellphone});
				layer.alert("发送成功！",{icon:1});
				$("#btnCode").hide();
				$(".count_time").addClass("inline_show");
				global.count = 60;
				countDown();//验证码读秒
			}
			
			//验证码读秒
			function countDown() {
				global.count--;
				if (global.count > 0) {
					$(".count_time").text(global.count + "秒后重新获取");
					setTimeout(countDown, 1000);
				} else {
					$("#btnCode").show();
					$(".count_time").hide();
					if (global.count == 0) {
						global.count = 60;
					}
				}
			}
		})
		
	},

	//提交时验证
	registValidation:function(){
		
		//判断用户名是否为空
		var account =$.trim($('#account').val());
		if(!account){
			layer.alert('用户账号不能为空！', {icon:2});
			return false;
		}
		if(global.accountExists){
			layer.alert('用户账号已存在！', {icon:2});
			return false;
		}
		
		//判断手机号
		var cellphone = $.trim($('#cellphone').val());
		if(!cellphone){
			layer.alert('手机号不能为空！', {icon:2});
			return false;
		}
		if(!telReg.test(cellphone)){
			layer.alert('手机号格式不正确！', {icon:2});
			return false;
		}
		if(global.cellphoneExists){
			layer.alert('手机号已注册！', {icon:2});
			return false;
		}
		
		//判断验证码
		var vercode = $.trim($('#vercode').val());
		if(!vercode){
			layer.alert('验证码不能为空！', {icon:2});
			return false;
		}
		if(!global.codeValid){
			layer.alert('验证码不正确！', {icon:2});
			return false;
		}
		if(cellphone!=global.phone){
			layer.alert('手机号与验证码不匹配！', {icon:2});
			return false;
		}
		
		//判断密码
		var password = $.trim($('#password').val());
		if(!password){
			layer.alert('密码不能为空！', {icon:2});
			return false;
		}
		if(password.length<6 || password.length>18){
			layer.alert('请设置密码长度为6-18位！', {icon:2});
			return false;
		}
		
		//判断确认密码
		var repassword = $.trim($('#repassword').val());
		if(repassword!=password){
			layer.alert('两次密码输入不一致！', {icon:2});
			return false;
		}

		return true;
	}

};

//验证密码强度
var verification_strength = {
		
	main : function(){

		//pwd事件触发：当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色   
	    $("#password").keyup(function() {  
	        pwStrength(this.value);  
	    });

	    //根据pwd强度改变css样式  
	    function pwStrength(pwd) {  
	        if (pwd == null || pwd == '') {  
	            return;
	        }else {
	            S_level = checkStrong(pwd);
	            $(".password_weak").removeClass("password_weak_color");
	            $(".password_in").removeClass("password_in_color");
	            $(".password_strong").removeClass("password_strong_color");
	            switch (S_level) {  
	  
	                case 0: //低  
	                	$(".password_weak").addClass("password_weak_color");
	                    break;  
	                case 1: //中  
	                	$(".password_in").addClass("password_in_color");  
	                    break;  
	                case 2: //高  
	                	$(".password_strong").addClass("password_strong_color"); 
	                    break;  
	                default:  
	                	$(".password_weak").addClass("password_weak_color");  
	            }  
	        }  
	        return;  
	    };

		function CharMode(iN) {  
	        if (iN >= 48 && iN <= 57) //数字   
	            return 1;  
	        if (iN >= 65 && iN <= 90) //大写字母   
	            return 2;  
	        if (iN >= 97 && iN <= 122) //小写   
	            return 4;  
	        else  
	            return 8; //特殊字符   
	    };
	    
	    //计算出当前密码当中一共有多少种模式   
	    function bitTotal(num) {  
	        modes = 0;  
	        for (i = 0; i < 4; i++) {  
	            if (num & 1) modes++;  
	            num >>>= 1;  
	        }  
	        return modes;  
	    };
	     
	    //返回密码的强度级别   
	    function checkStrong(sPW) {  
	        Modes = 0; //输入的字符种类有几种如：a1两种aA_d三种  
	        for (i = 0; i < sPW.length; i++) {  
	            //测试每一个字符的类别并统计一共有多少种模式.   
	            Modes |= CharMode(sPW.charCodeAt(i));  
	        }  
	        Modes = bitTotal(Modes); //由几种字符组成  
	        var pwdLength = sPW.length; //密码长度  
	        var level = 0; //密码强度级别  
	        if (pwdLength < 8 && Modes <= 2)  
	            level = 0;  
	        if ((pwdLength < 10 && Modes >= 3) || (pwdLength >= 8 && Modes == 2))  
	            level = 1;  
	        if (pwdLength >= 10 && Modes >= 3)  
	            level = 2;  
	        return level;  
	    };
	    
	}
};